/*
 *  Copyright 2012 yura.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */

package org.opu.pc.ui.client.actions;

import java.awt.event.ActionEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.Arrays;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.swing.JDialog;

import org.opu.pc.services.PublicationService;
import org.opu.pc.services.PublisherService;
import org.opu.pc.services.beans.AbstractBean;
import org.opu.pc.services.beans.PublicationBean;
import org.opu.pc.services.beans.PublisherBean;
import org.opu.pc.ui.client.MainView;
import org.opu.pc.ui.client.widgets.PropertiesEditPane;
import org.opu.pc.ui.client.widgets.PropertiesEditPane.PropertyOption;
import org.opu.pc.ui.util.Options;

/**
 *
 * @author yura
 */
public class Correct extends AbstractDialogAction 
		implements PropertyChangeListener {

	private static final long serialVersionUID = -3630423340171932188L;

	protected PublisherService publisherService;
	protected PublicationService publicationService;
	
	public void setPublisherService(PublisherService publisherService) {
		this.publisherService = publisherService;
	}

	public void setPublicationService(PublicationService publicationService) {
		this.publicationService = publicationService;
	}

    AbstractBean currentBean;

    @Override
    public void action(ActionEvent e) throws Exception {
        List<? extends AbstractBean> selected = mainView.getSelectedBeans();
        if (selected == null || selected.size() != 1){
            Options.error(getSafety(getName() + ".err1", "You need to select "
                    + "one publication or one publisher"));
            return;
        }
        
        currentBean = selected.get(0);

        super.action(e);
    }

    @Override
    protected int getMinimalDialogWidth() {
        if(currentBean instanceof PublisherBean){
            return 400;
        } else if (currentBean instanceof PublicationBean) {
            return 600;
        } else {
            return super.getMinimalDialogWidth();
        }
    }

    @Override
    protected void initPropMap(Map<String, List<PropertyOption>> map) {
        if(currentBean instanceof PublisherBean){
            map.put("isStudent", Arrays.asList(
				new PropertyOption("applied.true", "true", "true"),
				new PropertyOption("applied.false", "false", "false")));
        } else if (currentBean instanceof PublicationBean) {
            map.put("applied", Arrays.asList(
				new PropertyOption("applied.true", "true", "true"),
				new PropertyOption("applied.false", "false", "false")));
        }
    }

    @Override
    protected void initProp(LinkedHashMap<String, Object> properties) {
        properties.put("name", currentBean.getName());
        if(currentBean instanceof PublisherBean){
            PublisherBean publisher = (PublisherBean) currentBean;
            properties.put("secondName", publisher.getSurname());
            properties.put("lastName", publisher.getMiddleName());
            properties.put("isStudent", String.valueOf(publisher.isStudent()));
        } else if (currentBean instanceof PublicationBean) {
            PublicationBean bean = (PublicationBean) currentBean;
            properties.put("applied", String.valueOf(bean.isWasApplied()));
            properties.put("date", bean.getDate());
            properties.put("conference.venue", bean.getConferenceVenue());
            properties.put("page.number", bean.getPageNumberStart());
        }
    }

    @Override
    public String getName() {
        return "Correct";
    }

    @Override
    protected OkAction getOkAction(JDialog dialog, PropertiesEditPane pane) {
        return new OkAction(pane, dialog) {

			private static final long serialVersionUID = 6067038153296928823L;

			@Override
            public void runAction(ActionEvent e) throws Exception {
                hideDialog();
                Map<String, Object> prop = pane.getProperties();
                currentBean.setName((String)prop.get("name"));
                if (currentBean instanceof PublisherBean) {
                    PublisherBean publisher = (PublisherBean) currentBean;
                    publisher.setSurname((String)prop.get("secondName"));
                    publisher.setMiddleName((String)prop.get("lastName"));
                    publisher.setStudent(Boolean.parseBoolean(
                            (String)prop.get("isStudent")));
                    publisherService.updatePublisher(publisher);
                } else if (currentBean instanceof PublicationBean) {
                    PublicationBean publication = (PublicationBean) currentBean;
                    publication.setWasApplied(Boolean.parseBoolean(
                            (String)prop.get("applied")));
                    publication.setDate((Date)prop.get("date"));
                    publication.setConferenceVenue((String)prop.get("conference.venue"));
                    publication.setPageNumberEnd((Integer)prop.get("page.number"));
                    publicationService.updatePublication(publication);
                }
                mainView.update();
            }
        };
    }

	@Override
	public void setMainView(MainView mainView) {
		super.setMainView(mainView);
		mainView.addChangeListener(this);
	}

	@SuppressWarnings("rawtypes")
	@Override
	public void propertyChange(PropertyChangeEvent evt) {
		if (MainView.SELECTED_BEANS.equals(evt.getPropertyName())){
			List newValue = (List) evt.getNewValue();
			setEnabled(newValue != null && newValue.size() == 1);
		}
	}

}
